package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.CategorySearchDto;
import com.example.front.entity.Category;
import com.example.front.entity.Gateway;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface CategoryMapper {

    @Insert("INSERT INTO `category`" +
            " (`tenant_id`,`name`) VALUES" +
            " (#{category.tenantId}, #{category.name})")
    void create(@Param("category") Category category);

    @Update("UPDATE `category` SET `name` = #{category.name}" +
            " WHERE `id`=#{category.id} AND `tenant_id`=#{category.tenantId} AND `status`!=" + User.STATUS_DELETE)
    int update(@Param("category") Category category);

    @Update("UPDATE `category` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteById(long id, long tenantId);

    @Select("<script>" +
            "SELECT COUNT(*) FROM `category`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE+
            "</script>")
    long count(@Param("condition") CategorySearchDto condition);

    @Select("<script>" +
            "SELECT * FROM `category`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `id` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Category> findAll(@Param("condition") CategorySearchDto condition, @Param("pagination") Pagination pagination);

    @Select("SELECT * FROM category WHERE `tenant_id` = #{tenantId} AND `name` = #{name}  AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Category findByName(@Param("tenantId") long tenantId, @Param("name") String name);

    @Select("SELECT * FROM category WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Category findById(@Param("tenantId") long tenantId,@Param("id") Long id);

    @Select("<script>" +
            "SELECT * FROM `category` WHERE `id` IN " +
            "<foreach item='item' index='index' collection='categoryIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    List<Category> findCategoryByIds(@Param("categoryIds") List<Long> categoryIds);


    @Select("<script>" +
            "SELECT * FROM `category` " +
            "WHERE `id` = #{id}" +
            " AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    Category findbyId(@Param("id") Long id);

    @Select("<script>" +
            "SELECT e.name,e.id,b.id as bid FROM `category` as c , `event` as e , `button` as b" +
            " WHERE c.id = #{id} and b.category_id = c.id and  b.key = #{key} and e.id = b.event_id " +
            " AND c.status !=" + User.STATUS_DELETE +
            "</script>")
    Map<String,String> categoryEvent(@Param("id") Long id,@Param("key") Integer key);


}
